package com.eqdd.yiqidian.ui.mine.maker.fragment

import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.GridLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector
import com.eqdd.yiqidian.base.Config
import com.eqdd.yiqidian.bean.rebuild.HttpResultRows
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource
import com.eqdd.yiqidian.http.CustomLoadViewFactory
import com.eqdd.yiqidian.http.HttpConfig
import com.eqdd.yiqidian.http.JsonConverter
import com.eqdd.yiqidian.ui.discover.makerspace.MakerPhotoActivity
import com.eqdd.yiqidian.utils.ImageUtil
import com.luck.picture.lib.decoration.GridSpacingItemDecoration
import com.lzy.okgo.OkGo
import com.lzy.okrx2.adapter.FlowableBody
import com.shizhefei.mvc.MVCCoolHelper
import io.reactivex.Flowable
import kotlinx.android.synthetic.main.fragment_space.*
import org.jetbrains.anko.support.v4.startActivity
import java.io.Serializable
import java.util.*

/**
 * Created by jhj on 18-8-6.
 */
class MakerPhotoFragment : Fragment() {

    var slimAdapterEx: SlimAdapterEx<MakerPhotoBean>? = null
    var mvcHelper: MVCCoolHelper<List<MakerPhotoBean>>? = null
    private var makerGuid: String? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_space, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        var pageNum = 0
        val density = resources.displayMetrics.density
        val screenWidth = resources.displayMetrics.widthPixels
        val photoSize = (screenWidth - density * 30) / 2
        val params = LinearLayout.LayoutParams(photoSize.toInt(), photoSize.toInt())
        val list = arrayListOf<MakerPhotoBean>()
        makerGuid = arguments?.getString("makerGuid")
        recyclerView.layoutManager = GridLayoutManager(activity, 2)
        recyclerView.addItemDecoration(GridSpacingItemDecoration(2, 30, false));
        MVCCoolHelper.setLoadViewFractory(CustomLoadViewFactory().setShowFooter(false))
        mvcHelper = MVCCoolHelper<List<MakerPhotoBean>>(coolRefreshView)
        slimAdapterEx = SlimAdapterEx.create()
                .register<MakerPhotoBean>(R.layout.list_item_182, SlimInjector<MakerPhotoBean> { data, injector ->
                    injector
                            ?.with<ImageView>(R.id.imageView) {
                                it.layoutParams = params
                                ImageUtil.setImage(data?.imageUrl, it)
                            }
                            ?.text(R.id.tv_name, data?.title)
                            ?.text(R.id.tv_count, String.format(Locale.CHINA, "%d张", data?.imageCount))
                            ?.visible(R.id.linearLayout)
                            ?.gone(R.id.layout_addg)

                })
                .attachTo(recyclerView)
                .updateData(list as List<MakerPhotoBean>?) as SlimAdapterEx<MakerPhotoBean>?
        ItemClickSupport.addTo(recyclerView)
                .setOnItemClickListener { recyclerView, position, v ->
                    val data = slimAdapterEx?.getDataItem(position)
                    data?.let {
                        startActivity<MakerPhotoActivity>(Config.TITLE to it.title,
                                Config.ID to it.Id, "makerGuid" to it.creater, Config.TYPE to 1)
                    }

                }


        val dataSource = ModelRx2DataSource<MakerPhotoBean>(ModelRx2DataSource.OnLoadSource<MakerPhotoBean> { page, register ->
            if (page == 1) {
                pageNum = 0
            }
            OkGo.post<HttpResultRows<MakerPhotoBean>>(HttpConfig.BASE_URL + HttpConfig.PHOTO_MENU_LIST)
                    .params("page", pageNum)
                    .params("lectureGuid", makerGuid)
                    .converter(object : JsonConverter<HttpResultRows<MakerPhotoBean>>() {
                        override fun test() {
                            super.test()
                        }
                    })
                    .adapt(FlowableBody<HttpResultRows<MakerPhotoBean>>())
                    .flatMap { result ->
                        if (page == 1) {
                            mvcHelper?.isAutoLoadMore = true
                        }
                        pageNum = result.items.page

                        val dataList = result.items.rows ?: arrayListOf<MakerPhotoBean>()
                        Flowable.just<MutableList<MakerPhotoBean>>(
                                dataList as MutableList<MakerPhotoBean>?
                                        ?: mutableListOf<MakerPhotoBean>()
                        )
                    }

        }, 9)
        mvcHelper?.setDataSource(dataSource)
        mvcHelper?.adapter = slimAdapterEx
        mvcHelper?.refresh()


    }

    override fun onDestroy() {
        super.onDestroy()
        MVCCoolHelper.setLoadViewFractory(CustomLoadViewFactory().setShowFooter(true))
    }

    class MakerPhotoBean : Serializable {
        //左侧图片
        var Id: Int = 0
        var imageCount: Int = 0
        var creater: String = ""
        var title: String = ""
        var imageUrl: String = ""
        var isAdd: Boolean = false//设置新增图片

    }

}